/***************************************************************************
 *   Copyright (C) 2008 by Branan Riley   *
 *   branan@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "gamedialog.h"

#include <QSettings>
#include <QFileDialog>

GameDialog::GameDialog(QWidget *parent)
 : QDialog(parent)
{
  setupUi(this);
  setModal(true);

  _settings = new QSettings(this);

  int lastGame = _settings->value("common/game_id", 0).toInt();

  if(gamebox->currentIndex() != lastGame) {
    gamebox->setCurrentIndex(lastGame);
  } else {
    QString game = gamebox->currentText();

    QString path = _settings->value(game + "/engine_path", "").toString();
    QString fs_game = _settings->value(game +"/fs_game", "").toString();
    QString fs_game_base = _settings->value(game + "/fs_game_base", "").toString();

    enginepath->setText(path);
    fsgame->setText(fs_game);
    fsgamebase->setText(fs_game_base);
  }
}


GameDialog::~GameDialog()
{
}

QString GameDialog::game()
{
  return gamebox->currentText();
}

QString GameDialog::gameDir()
{
  return enginepath->text();
}

QString GameDialog::fsGame()
{
  return fsgame->text();
}

QString GameDialog::fsGameBase()
{
  return fsgamebase->text();
}

void GameDialog::on_browse_clicked()
{
  enginepath->setText(QFileDialog::getExistingDirectory(this, "Engine Path",
                                                        enginepath->text()));
}

void GameDialog::on_gamebox_currentIndexChanged(int index)
{
  QString game = gamebox->currentText();

  QString path = _settings->value(game + "/engine_path", "").toString();
  QString fs_game = _settings->value(game +"/fs_game", "").toString();
  QString fs_game_base = _settings->value(game + "/fs_game_base", "").toString();

  enginepath->setText(path);
  fsgame->setText(fs_game);
  fsgamebase->setText(fs_game_base);

  _settings->setValue("common/game_id", index);
}

void GameDialog::on_enginepath_textChanged(const QString &text)
{
  QString game = gamebox->currentText();

  _settings->setValue(game + "/engine_path", text);
}

void GameDialog::on_fsgame_textChanged(const QString &text)
{
  QString game = gamebox->currentText();

  _settings->setValue(game + "/fs_game", text);
}

void GameDialog::on_fsgamebase_textChanged(const QString &text)
{
  QString game = gamebox->currentText();

  _settings->setValue(game + "/fs_game_base", text);
}
